//
//  ZJWebViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/23.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJWebViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface ZJWebViewController ()<UIWebViewDelegate>
{
    NSString *_netWorkingType;
}
/** webView */
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation ZJWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"加载中...";
    self.webView.scrollView.bounces = NO;
    if (self.isHiddenNav) {
        self.webView.hidden = YES;
    }
    [self.view addSubview:self.webView];
    if (self.isHiddenNav) {
        UIView * tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], LP_StatusBarHeight)];
        tempView.backgroundColor = [UIColor themeColor];
        [self.view addSubview:tempView];
    }
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
    [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
}



- (void)action_leftButton:(UIButton *)sender{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }else{
        if (self.isModel) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}
#pragma mark - UIWebView 协议方法
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //获取当前页面的标题
    NSString * tempString = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    if (self.titleString.length >  0) {
        self.titlelabel.text = self.titleString;
    }
    else{
        self.titlelabel.text = tempString;
    }
    [webView stringByEvaluatingJavaScriptFromString:@"window.setTitle()"];
//
//    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.setTitle('%.2lf')" , (LP_NavViewHeight / [UIScreen zj_screenWith])] ];
//
    
    NSString *injectionJSString = @"var script = document.createElement('meta');"
    "script.name = 'viewport';"
    "script.content=\"width=device-width, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    [webView stringByEvaluatingJavaScriptFromString:injectionJSString];
//
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    context[@"setTitle"] = ^{
        NSArray *infor = [JSContext currentArguments];
        JSValue *imageString = infor.firstObject;
        NSString *title = [imageString toString];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (title) {
                self.titlelabel.text = title;
            }
            
        });
    };
    context[@"exitCurrentPage"] = ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
            
        });
        
    };
    if (self.isHiddenNav) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
            self.webView.hidden = NO;
        });
    }
    else{
        [YJProgressHUD hide];
    }

}



- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"9999999%@",error.localizedDescription);
}
#pragma mark - Setter


- (void)setUrlString:(NSString *)urlString{
    _urlString = urlString;
    
}

#pragma mark - Getters

- (UIWebView *)webView{
    if (_webView == nil){
        CGRect rect = CGRectMake(0, LP_NavViewHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_NavViewHeight);
        if (self.isHiddenNav) {
            rect = CGRectMake(0, LP_StatusBarHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_StatusBarHeight);
        }
        _webView = [[UIWebView alloc]initWithFrame:rect];
        _webView.delegate = self;
        _webView.scalesPageToFit=YES;
        _webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber | UIDataDetectorTypeLink | UIDataDetectorTypeShipmentTrackingNumber | UIDataDetectorTypeFlightNumber | UIDataDetectorTypeLookupSuggestion;
        _webView.backgroundColor = [UIColor clearColor];
        [_webView setOpaque:YES];
    }
    return _webView;
}
@end
